var pl = {};
if (pl === undefined) {
    pl = {};
}
if (pl.beabeleza === undefined) {
    pl.beabeleza = {};
}
pl.beabeleza.const = {
    TEXT_LENGTH_IN_BREADCRUMB: 50,
    TEMPLATE_PRICE: '#price',
    TEMPLATE_SAVED_MONEY: '#savedMoney',
    TEMPLATE_AMOUNT_INPUT: '#amountInput',
    PRODUCT_HTML_TEMPLATE:
            '<div class="contentBox_">' +
            '<div class="newProduct_">Nowość!</div>' +
            '<div class="priceBox_">' +
            '    <div class="ourPrice_">Nasza cena:</div>' +
            '    <div class="price_">#price zł</div>' +
            '    <div class="saveMoney">*Oszczędzasz: #savedMoney</div>' +
            '</div>' +
            '<div class="shoppingCartBox_">' +
            '    <div class="amount_">Ilość : ' +
            '        #amountInput szt.' + //<input value="1">#amountInput szt.'
            '        <div class="shoppingCart_">Dodaj do koszyka</div>' +
            '        <div class="productAvailability_">Towar dostępny - ' +
            '            <a href="#">natychmiatowa wysyłka</a>' +
            '        </div>' +
            '    </div>' +
            '</div>' +
            '</div>' +
            '<div class="contentBox_" style="border-top: 0px;">' +
            '   <div class="title_">Producent' +
            '       <span class="productDetail_">Goldwell</span>' +
            '   </div>' +
            '   <div class="title_">Seria' +
            '       <span class="productDetail_">Scalap regulation </span>' +
            '       <a href="#" class="titleFont_"> zobacz inne produkty</a>' +
            '   </div>' +
            '   <div class="title_">Wasza ocena' +
            '       <span class="titleFont_">Opinie : 15</span>' +
            '       <a href="#" class="titleFont_"> Dodaj własną.</a>' +
            '   </div>' +
            '   <div class="title_">' +
            '       Poleć znajomemuv' +
            '   </div>' +
            '</div>'
};
pl.beabeleza.utils = {
    cutTextIfIsTooLong: function(text) {
        if (text.length > pl.beabeleza.const.TEXT_LENGTH_IN_BREADCRUMB) {
            text = text.substring(0, pl.beabeleza.const.TEXT_LENGTH_IN_BREADCRUMB) + '...';
        }
        return text;
    },
    replacePriceNotNeededChars: function(price) {
        return price.replace(/\s/gi, '').replace('PLN', '').replace('(', '').replace(')', '').replace(',', '.');
    }
};

pl.beabeleza.Product = {
    fitTemplate: function() {
        var me = this;

        var price = me.getPrice();
        var oldPrice = me.getOldPrice();
        var savedMoney = (parseFloat(oldPrice) - parseFloat(price)).toFixed(2);
        me.addToTemplate(pl.beabeleza.const.TEMPLATE_PRICE, price);
        me.addToTemplate(pl.beabeleza.const.TEMPLATE_SAVED_MONEY, savedMoney);
        me.addToTemplate(pl.beabeleza.const.TEMPLATE_AMOUNT_INPUT, me.getAmountInputHtml());
    },
    getHtmlTemplate: function() {
        return pl.beabeleza.const.PRODUCT_HTML_TEMPLATE;
    },
    addToTemplate: function(key, value) {
        pl.beabeleza.const.PRODUCT_HTML_TEMPLATE =
                pl.beabeleza.const.PRODUCT_HTML_TEMPLATE.replace(key, value);
    },
    setBreadcrumb: function() {
        var ul = $("<ul></ul>");
        var text = $('.pagerPath').text().replace(/>/g, '');
        text = pl.beabeleza.utils.cutTextIfIsTooLong(text);
        var pager = $('.pagerPath a').clone();
        pager.each(function(key, value) {
            var productCategory = pl.beabeleza.utils.cutTextIfIsTooLong($(value).text());
            $(value).text(productCategory);
            var li = $('<li></li>').append(value);
            ul.append(li);
        });
        var li = $('<li></li>');
        var a = $('<a href="#" onclick="return false;"></a>').text(text);
        li.append(a);
        ul.append(li);
        var newPagerPath = $('<div></div>');
        newPagerPath.addClass('pagerPath');
        $('.pagerPath').remove();
        newPagerPath.append(ul);
        newPagerPath.addClass('breadcrumbs-one');
        $('.pager').append(newPagerPath);
    },
    setHeader: function() {
        $('.productName span').css({"color": "#d11163", "font-family": "'Average Sans',sans-serif", "font-size": "20px", 'font-weight': 'normal'});
    },
    hideProductPager: function() {
        $('.pagerPos').hide();
    },
    hidePromotion: function() {
        $('.hotSection').hide();
    },
    getPrice: function() {
        var price = $($('.price')[0]);
        return pl.beabeleza.utils.replacePriceNotNeededChars(price.text());
    },
    getAmountInputHtml: function() {
        var input = $('.productAddToCart input[type=text]');
        var name = input.attr('name');
        var value = input.attr('value');
        var id = input.attr('id');
        return '<input name="' + name + '" id="' + id + '" value="' + value + '" />  ';
    },
    getOldPrice: function() {
        var oldPrice = $($('.ps_priceOld')[1]);
        if (oldPrice === undefined) {
            return 0;
        }
        return pl.beabeleza.utils.replacePriceNotNeededChars(oldPrice.text());
    },
    showPage: function() {
        $('body').show();
    }
};

$(function() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(prodRequestHandler);
    prodDoWork();
});
function prodRequestHandler(sender, args) {
    prodDoWork();
}

function prodDoWork() {
    menuDoWork();
    cartDoWork();
    pl.beabeleza.Product.setHeader();
    pl.beabeleza.Product.setBreadcrumb();
    pl.beabeleza.Product.hideProductPager();
    pl.beabeleza.Product.showPage();
}
